package baseball.controller;

import baseball.domain.Activity;
import baseball.domain.Course;
import baseball.domain.User;
import baseball.mapper.ActivityMapper;
import baseball.mapper.CourseMapper;
import baseball.mapper.UserMapper;
import baseball.support.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController extends BaseController {


    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("index")
    public String index(Model model,HttpSession session) {
        //公告列表
        List<Activity> activityList = activityMapper.selectAll();
        //课程列表
        Course course = new Course();
        course.setStatus("通过");
        List<Course> courseList = courseMapper.select(course);

        session.setAttribute("activityList", activityList);
        model.addAttribute("courseList", courseList);
        return "index";
    }

    /**
     * 到注册页面
     *
     * @return
     */
    @GetMapping("register")
    public String register() {
        return "register";
    }

    /**
     * 注册用户
     *
     * @return
     */
    @PostMapping("register")
    public String register(User user) {
        user.setAddtime(new Date());
        userMapper.insert(user);
        return "redirect:index.do";
    }

    /**
     * 登录页面
     * @return
     */
    @GetMapping("login")
    public String login() {
        return "login";
    }
    /**
     * 登录
     * @return
     */
    @PostMapping("login")
    public String login(String username, String password, HttpSession session,Model model){
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user = userMapper.selectOne(user);
        if(null == user){
            model.addAttribute("msg","用户名或密码错误");
            return "login";
        }
        session.setAttribute("sessionUser",user);
        return "redirect:index.do";
    }

    /**
     * 公告详情
     * @param id
     * @return
     */
    @RequestMapping("notice/{id}")
    public String notice(@PathVariable Integer id,Model model){
        Activity activity = activityMapper.selectByPrimaryKey(id);
        model.addAttribute(activity);
        return "notice";
    }

    /**
     * 退出系统
     * @return
     */
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("sessionUser");
        return "redirect:login.do";
    }

    /**
     * 详情
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id,Model model,HttpSession session){
        Course course = courseMapper.selectByPrimaryKey(id);
        model.addAttribute(course);
        User user = (User) session.getAttribute("sessionUser");
        if(null != user){
            Integer i = courseMapper.selectOrder(user.getId(), id);
            model.addAttribute("st",i);
        }
        return "detail";
    }
}
